package com.mysoft.gfyx.marketdevelop.controller;

import com.alibaba.fastjson.JSONArray;
import com.mysoft.framework.common.exception.BusinessLogicException;
import com.mysoft.framework.modeling.dataapi.DataApi;
import com.mysoft.framework.mvc.api.Controller;
import com.mysoft.framework.rpc.annotation.PubAction;
import com.mysoft.framework.rpc.annotation.PubService;
import com.mysoft.framework.rpc.contants.RequestPrefix;

import com.mysoft.gfyx.marketdevelop.controller.dto.advancesearch.*;
import com.mysoft.gfyx.marketdevelop.mip.GetDeveloperDataService;
import com.mysoft.gfyx.marketdevelop.mip.dto.BaseTjFilterDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.BaseTjListDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.BaseTjListResponseDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.GetDeveloperFilterDataRequestDto;
import com.mysoft.gfyx.marketdevelop.service.AdvanceSearchService;
import com.mysoft.gfyx.marketdevelop.service.DeveloperService;
import com.mysoft.gfyx.marketdevelop.service.dto.AdvanceSearchUserSavedDetailDto;
import com.mysoft.gfyx.marketdevelop.service.dto.AdvanceSearchUserSavedDto;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.UUID;

/**
 * 高级筛选控制器
 */
@PubService(value = "/advanceSearchDataApi", prefix = RequestPrefix.API)
@Tag(name = "高级筛选-API")
public class AdvanceSearchDataApiController extends Controller implements DataApi {
    @Resource
    private DeveloperService developerService;

    @Resource
    private AdvanceSearchService advanceSearchService;

    //获取高级筛选字段组+字段元数据
    @PubAction(value = "/getAdvanceSearchFieldGroup", method = RequestMethod.POST)
    public AdvanceSearchFieldGroupDto getAdvanceSearchFieldGroup() {
        return advanceSearchService.getAdvanceSearchFieldGroup();
    }

    //高级筛选-保存筛选模板
    @PubAction(value = "/saveAdvanceSearchTemplate", method = RequestMethod.POST)
    public UUID saveAdvanceSearchTemplate(@RequestBody SaveAdvanceSearchTemplateRequestDto saveAdvanceSearchTemplateRequestDto) {
        return advanceSearchService.saveAdvanceSearchTemplate(saveAdvanceSearchTemplateRequestDto);
    }

    //高级筛选-获取保存的筛选模板列表
    @PubAction(value = "/getSavedAdvanceSearchTemplate", method = RequestMethod.POST)
    public List<AdvanceSearchUserSavedDto> getSavedAdvanceSearchTemplate() {
        return advanceSearchService.getSavedAdvanceSearchTemplate();
    }

    //高级筛选-获取保存的筛选模板详情
    @PubAction(value = "/getSavedAdvanceSearchTemplateDetail", method = RequestMethod.POST)
    public GetSavedAdvanceSearchTemplateDetailResponseDto getSavedAdvanceSearchTemplateDetail(UUID advanceSearchUserSavedGUID) {
        AdvanceSearchUserSavedDto advanceSearchUserSavedDto = advanceSearchService.getSavedAdvanceSearchTemplateById(advanceSearchUserSavedGUID);
        if (advanceSearchUserSavedDto == null) {
            throw new BusinessLogicException("模板不存在");
        }
        GetSavedAdvanceSearchTemplateDetailResponseDto getSavedAdvanceSearchTemplateDetailResponseDto = new GetSavedAdvanceSearchTemplateDetailResponseDto();
        getSavedAdvanceSearchTemplateDetailResponseDto.setTemplateName(advanceSearchUserSavedDto.getTemplateName());
        List<AdvanceSearchUserSavedDetailDto> advanceSearchUserSavedDetailDtoList = advanceSearchService.getSavedAdvanceSearchTemplateDetail(advanceSearchUserSavedGUID);
        List<FiltersDto> filtersDtoList = new ArrayList<>();
        for (AdvanceSearchUserSavedDetailDto advanceSearchUserSavedDetailDto : advanceSearchUserSavedDetailDtoList) {
            List<String> actionValue = JSONArray.parseArray(advanceSearchUserSavedDetailDto.getActionValue(), String.class);
            if (advanceSearchUserSavedDetailDto.getSort() == 0 && advanceSearchUserSavedDetailDto.getFieldName().equals("relative")) {
                getSavedAdvanceSearchTemplateDetailResponseDto.setRelative(Integer.valueOf(actionValue.get(0)));
            }
            FiltersDto filtersDto = new FiltersDto();
            filtersDto.setActionCode(advanceSearchUserSavedDetailDto.getActionCode());
            filtersDto.setActionValue(actionValue);
            filtersDto.setAttributeName(advanceSearchUserSavedDetailDto.getFieldName());
            filtersDto.setTableName(advanceSearchUserSavedDetailDto.getTableName());
            filtersDtoList.add(filtersDto);
        }
        getSavedAdvanceSearchTemplateDetailResponseDto.setFilters(filtersDtoList);

        //使用次数+1
        advanceSearchService.addAdvanceSearchTemplateUseCount(advanceSearchUserSavedDto);
        return getSavedAdvanceSearchTemplateDetailResponseDto;
    }

    //高级筛选-删除保存的模板
    @PubAction(value = "/delSavedAdvanceSearchTemplate", method = RequestMethod.POST)
    public boolean delSavedAdvanceSearchTemplate(UUID advanceSearchUserSavedGUID) {
        AdvanceSearchUserSavedDto advanceSearchUserSavedDto = advanceSearchService.getSavedAdvanceSearchTemplateById(advanceSearchUserSavedGUID);
        if (advanceSearchUserSavedDto == null) {
            throw new BusinessLogicException("模板不存在");
        }
        return advanceSearchService.delSavedAdvanceSearchTemplate(advanceSearchUserSavedGUID);
    }

    //高级筛选-找客户
    @PubAction(value = "/searchCustomer", method = RequestMethod.POST)
    public Object searchCustomer(@RequestBody AdvanceSearchCustomerRequestDto advanceSearchCustomerRequestDto) throws Exception {
        Object response = advanceSearchService.searchCustomer(advanceSearchCustomerRequestDto);
        return response;
    }

    //高级筛选-找商机
    @PubAction(value = "/searchOpptyPurchase", method = RequestMethod.POST)
    public Object searchOpptyPurchase(@RequestBody AdvanceSearchCustomerRequestDto advanceSearchCustomerRequestDto) throws Exception {
        Object response = advanceSearchService.searchOpptyPurchase(advanceSearchCustomerRequestDto);
        return response;
    }


}
